package web.suzy.oj.manager.admin.announcement;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import web.suzy.oj.common.exception.StatusFailException;
import web.suzy.oj.dao.common.AnnouncementEntityService;
import web.suzy.oj.pojo.entity.common.Announcement;
import web.suzy.oj.pojo.vo.AnnouncementVO;
import web.suzy.oj.shiro.AccountProfile;

/**
 * YangSuzy 软件工作室
 * 类名: AdminAnnouncementManager
 * 描述: ---- 待定 -----
 * 功能: ---- 待定 -----
 *
 * @author YangSuzy
 * Date: 2022/11/25 17:59
 */
@Component
@Slf4j
public class AdminAnnouncementManager {
    @Autowired
    private AnnouncementEntityService announcementEntityService;

    /**
     * 方法名: getAnnouncementList
     * 描述: 获取管理员发布的公告列表
     *
     * @param limit       每页显示数量
     * @param currentPage 当前页
     * @return com.baomidou.mybatisplus.core.metadata.IPage<web.suzy.oj.pojo.vo.AnnouncementVO>
     * @date 2022/11/25 17:59
     * @auther YangSuzy
     **/
    public IPage<AnnouncementVO> getAnnouncementList(Integer limit, Integer currentPage) {
        //设置默认当前页和每页默认显示数目
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        if (limit == null || limit < 1) {
            limit = 10;
        }
        return announcementEntityService.getAnnouncementList(limit, currentPage, false);
    }

    /**
     * 方法名: addAnnouncement
     * 描述: 管理员发布公告
     *
     * @param announcement 待发布公告
     * @return void
     * @date 2022/11/25 18:00
     * @auther YangSuzy
     **/
    public void addAnnouncement(Announcement announcement) throws StatusFailException {
        boolean isOk = announcementEntityService.save(announcement);
        if (!isOk) {
            throw new StatusFailException("添加失败");
        }
    }

    /**
     * 方法名: updateAnnouncement
     * 描述: 管理员更新公告
     *
     * @param announcement 待更新公告
     * @return void
     * @date 2022/11/25 18:01
     * @auther YangSuzy
     **/
    public void updateAnnouncement(Announcement announcement) throws StatusFailException {
        boolean isOk = announcementEntityService.saveOrUpdate(announcement);
        if (!isOk) {
            throw new StatusFailException("修改失败");
        }
    }

    /**
     * 方法名: deleteAnnouncement
     * 描述: 管理员删除公告
     *
     * @param aid 公告ID
     * @return void
     * @date 2022/11/25 18:01
     * @auther YangSuzy
     **/
    public void deleteAnnouncement(long aid) throws StatusFailException {
        boolean isOk = announcementEntityService.removeById(aid);
        if (!isOk) {
            throw new StatusFailException("删除失败");
        }
        //记录执行删除操作的管理员
        AccountProfile userRolesVo = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        log.info("[{}],[{}],id:[{}],operatorUid:[{}],operatorUsername:[{}]",
                "Admin_Announcement", "Delete", aid, userRolesVo.getUid(), userRolesVo.getUsername());
    }
}
